package com.rzhd.coursepatriarch.common.view;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatButton;
import android.util.AttributeSet;
import android.util.Log;

import com.rzhd.coursepatriarch.common.R;

import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * 倒计时按钮
 *
 * @author yql
 * Time: 2018/6/5.
 */
public class TimerButton extends AppCompatButton {

    /**
     * 上下文对象
     */
    private Context mContext;

    /**
     * 倒计时长度，默认60秒
     */
    private long maxTime = 60;
    /**
     * 倒计时按钮未按下，显示的文本
     */
    private String textDefault;

    /**
     * 默认背景框
     */
    private Drawable normalBackground;

    private Drawable clickBackground;
    /**
     * 倒计时按钮已按下，显示的文本
     */
    private String textAfter = "秒";

    private Disposable mDisposable;

    public TimerButton(Context context) {
        this(context, null);
    }

    public TimerButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TimerButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initAttr(attrs);
    }

    private void initAttr(AttributeSet attrs) {
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.TimerButton);
        textDefault = typedArray.getString(R.styleable.TimerButton_text_default);
        normalBackground = typedArray.getDrawable(R.styleable.TimerButton_normal_background);
        clickBackground = typedArray.getDrawable(R.styleable.TimerButton_click_background);
        typedArray.recycle();
        this.setBackgroundDrawable(normalBackground);
        this.setText(textDefault);
    }

    /**
     * 清除对象
     */
    public void clearTimer() {
        if (mDisposable != null) {
            mDisposable.dispose();
        }
        //倒计时完毕置为可点击状态
        TimerButton.this.setEnabled(true);
        TimerButton.this.setText(textDefault);
        TimerButton.this.setBackgroundDrawable(normalBackground);
    }

    /**
     * 开始倒计时
     */
    public void startTimer() {
        this.setEnabled(false);
        this.setBackgroundDrawable(clickBackground);
        mDisposable = Flowable.intervalRange(0, maxTime, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        Log.e("this", "aLong" + aLong);
                        TimerButton.this.setText((maxTime - aLong) + textAfter);
                    }
                }).doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {
                        clearTimer();
                    }
                }).subscribe();

    }

}
